﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using WheelClean2.Mould;

namespace WheelClean2.Model
{
    /// <summary>
    ///PLC设备列表
    /// </summary>
    public class PLCInfos : BindableBase
    {
        public int ID { get; set; }
        /// <summary>
        /// 设备名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 设备地址
        /// </summary>
        public string Address { get; set; }
        [NotMapped]
        private int _addressSort;
        /// <summary>
        /// 设备列表
        /// </summary>
        [NotMapped]
        public int AddressSort
        {
            get { return _addressSort; }
            set { SetProperty(ref _addressSort, value); }
        }
        [NotMapped]
        private string _data;
        /// <summary>
        /// 数据
        /// </summary>
        [NotMapped]
        public string Data
        {
            get { return _data; }
            set { SetProperty(ref _data, value); }
        }

        [NotMapped]
        private bool _isEnabled;
        /// <summary>
        /// 是否使用
        /// </summary>
        [NotMapped]
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set { SetProperty(ref _isEnabled, value); }
        }
        [NotMapped]
        private string _input;
        /// <summary>
        /// 输入
        /// </summary>
        [NotMapped]
        public string Input
        {
            get { return _input; }
            set { SetProperty(ref _input, value); }
        }



        /// <summary>
        /// 操作类型
        /// </summary>
        public string Type { get; set; }
        /// <summary>
        /// 数据类型
        /// </summary>
        public string DataType { get; set; }
        /// <summary>
        /// 父设备
        /// </summary>

        public int? Parent { get; set; }

        /// <summary>b
        /// R可读  W 可写
        /// </summary>
        public string Operate { get; set; }

        

        /// <summary>
        /// 测试链接
        /// </summary>
        [NotMapped]
        public DelegateCommand DelegateCommand_Check { get; private set; }

        PLCInfos()
        {
            DelegateCommand_Check = new DelegateCommand(Check);
        }

        /// <summary>
        /// 功能测试
        /// </summary>
        private void Check()
        {
            Thread thread = new Thread(() =>
            {


                //如果是线圈
                if (Address.Contains("0X"))
                {
                    ushort address = Convert.ToUInt16(Address.Replace("0X", ""));
                    //上升沿
                    if (this.Type.Contains("上升沿"))
                    {

                        ModbusTCP.WriteSingleCoil(address, true);
                        Thread.Sleep(300);
                        ModbusTCP.WriteSingleCoil(address, false);
                    }
                    //按下保持
                    else if (this.Type.Contains("按下保持"))
                    {

                        ModbusTCP.WriteSingleCoil(address, true);
                        int a = 0;
                        int.TryParse(Input, out a);
                        Thread.Sleep(a);
                        ModbusTCP.WriteSingleCoil(address, false);
                    }
                    else
                    {
                        if(Convert.ToBoolean(Data)==true )
                        {
                            ModbusTCP.WriteSingleCoil(address, false);
                        }
                        else
                        {
                            ModbusTCP.WriteSingleCoil(address, true);
                        }
                    }

                }
                //如果是寄存器
               else 
                {
                    //MessageBox.Show("123");
                    ushort address = Convert.ToUInt16(Address.Replace("4X", ""));
                    if(DataType.Contains("INT"))
                    {
                        int a = 0;
                        int.TryParse(Input, out a);
                        ModbusTCP.WriteSingleRegister(address, (ushort)a);
                    }
                    else
                    {
                        float a = 0;
                        float.TryParse(Input, out a);
                        ModbusTCP.WriteFloat(address, a);
                    }
                    

                }

            });
            thread.Start();
            
        }
    }
}

